package com.zzh.rabbit.info;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.zzh.lib.bar.java.UltimateBarX;
import com.zzh.lib.core.lang.ObjectUtils;
import com.zzh.lib.core.listener.HOnClickListener;
import com.zzh.lib.core.utils.HDeviceUtils;
import com.zzh.lib.core.utils.HFileUtils;
import com.zzh.lib.core.utils.HNetUtils;
import com.zzh.lib.core.utils.HResUtils;
import com.zzh.lib.core.utils.HTaskUtils;
import com.zzh.lib.core.utils.LogUtils;
import com.zzh.lib.core.utils.ToastUtils;
import com.zzh.lib.log.Logger;
import com.zzh.rabbit.MainViewModel;
import com.zzh.rabbit.R;
import com.zzh.rabbit.User;
import com.zzh.rabbit.databinding.ActDeviceInfoBinding;
import com.zzh.rabbit.demo.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;

/**
 * Created by ZZH on 2022/2/28.
 *
 * @Date: 2022/2/28
 * @Email: zzh_hz@126.com
 * @QQ: 1299234582
 * @Author: zzh
 * @Description: 设备信息
 */
public class DeviceInfoActivity extends BaseActivity<MainViewModel, ActDeviceInfoBinding> {

    @Override
    protected void initViewAndData(Bundle savedInstanceState) {
        super.initViewAndData(savedInstanceState);
        setSupportActionBar(bind.toolbar);
        getSupportActionBar().setTitle("设备参数");

        StringBuilder builder = new StringBuilder();
        builder.append("Android Version：\t\t").append(Build.VERSION.RELEASE).append(" ").append(Build.VERSION.SDK_INT).append("\n");
        builder.append("设备名称：\t\t").append(HDeviceUtils.getDeviceDevice()).append("\n");
        builder.append("手机型号：\t\t").append(HDeviceUtils.getDeviceModel()).append("\n");
        builder.append("手机(运存)RAM：\t\t").append(HDeviceUtils.getTotalRAM()).append("\n");
        builder.append("手机可用运存：\t\t").append(HDeviceUtils.getAvailableRAM()).append("\n");


        builder.append("屏幕尺寸宽：\t\t").append(HResUtils.getDisplayWidth()).append("\n");
        builder.append("屏幕尺寸高：\t\t").append(HResUtils.getDisplayHeight()).append("\n");

        builder.append("ActionBarSize：\t\t").append(HResUtils.getActionBarHeight()).append("\n");
        builder.append("StatusBarSize：\t\t").append(HResUtils.getStatusBarHeight()).append("\n");
        builder.append("Nav Bar Size：\t\t").append(UltimateBarX.getNavigationBarHeight()).append("\n");
        //builder.append("Nav Bar Size Imm：\t\t").append(ImmersionBar.getNavigationBarHeight(this)).append("\n");

        builder.append("tags：\t\t").append(Build.TAGS).append("\n");
        builder.append("user：\t\t").append(Build.USER).append("\n");
        builder.append("SERIAL：\t\t").append(Build.SERIAL).append("\n");
        builder.append("BRAND：\t\t").append(Build.BRAND).append("\n");
        builder.append("BOARD：\t\t").append(Build.BOARD).append("\n");
        builder.append("CPU：\t\t").append(HDeviceUtils.getCPUAbi()).append("\n");
        try {
            builder.append("IP：\t\t").append(HNetUtils.getIpv4Address()).append("\n");
        } catch (Exception ex) {

        }

        WebView webView = new WebView(this);
        builder.append("WebView UA：\t\t").append(webView.getSettings().getUserAgentString()).append("\n");
        LogUtils.d("这是一个测试用例日志");
        bind.tvDeviceInfo.setText(builder.toString());
        Logger.getInstance().setExceptionLogEnabled(true);
        Logger.getInstance().setFilePath(HFileUtils.getPublicDownloadFile().getAbsolutePath() + "/HRabbit/log" +
                "/device.txt");
        if (XXPermissions.isGrantedPermissions(this, Permission.MANAGE_EXTERNAL_STORAGE)) {
            Logger.log("这是一个测试用例日志");
            Logger.log(builder.toString());
        } else {
            XXPermissions.with(this)
                    .permission(Permission.MANAGE_EXTERNAL_STORAGE)
                    .request((list, b) -> {
                        if (b) {
                            Logger.log("这是一个测试用例日志");
                            Logger.log(builder.toString());
                        } else {
                            ToastUtils.show("没有存储权限");
                        }
                    });
        }

        getAppList();
    }


    @Override
    public void setListener() {
        super.setListener();
        bind.mbLog.setOnClickListener(v -> {
            Logger.getInstance().setFilePath(HFileUtils.getPublicDownloadFile().getAbsolutePath() + "/HRabbit/log" +
                    "/app_detail.txt");
            String string = bind.tvDeviceInfo.getText().toString();
            Logger.log(string);
            LogUtils.d(string);
            String message = bind.tvAppList.getText().toString();
            Logger.log(message);
            LogUtils.d(message);
            HTaskUtils.getDefaultInstance().execute(() -> {
                int count = 10;
                for (int i = 0; i < 10; i++) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        LogUtils.e(e);
                    }
                    Logger.log("这是第" + i + "条日志， " + System.currentTimeMillis() + ", 线程：" + Thread.currentThread().getName());
                }
                LogUtils.d("这是" + count + "条日志，写入完成");
            }, 100);
        });
    }

    void getAppList() {
        HTaskUtils.getDefaultInstance().execute(() -> {
            List<PackageInfo> installedPackages = getPackageManager().getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
            if (!installedPackages.isEmpty()) {
                StringBuilder sb = new StringBuilder(
                        "\n\n\n\n\n"
                );
                for (PackageInfo pi : installedPackages) {
                    String appName;
                    if (pi.applicationInfo != null) {
                        appName = pi.applicationInfo.name;
                    } else {
                        appName = "没有名称";
                    }
                    String versionName = pi.versionName;
                    String versionCode = String.valueOf(pi.versionCode);
                    String packageName = pi.packageName;
                    String info = appName + "\nversionName" + versionName + ", versionCode: " + versionCode + ",\npackageName: " + packageName + "\n";
                    sb.append(info).append("\n");
                }
                runOnUiThread(() -> {
                    bind.tvAppList.setText(sb.toString());
                });
            } else {
                LogUtils.e("没有程序");
            }
        });
    }
}
